import requests as req;
import os;

def get_list(url,params):

    # UA伪装
    header = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
        "Mobile-User-Agent":"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36"
    }

    result = req.get(url, params=params, headers=header);
    json = result.json();

    if len(json) > 0:
        if len(json) > 0 :

            file_name = "爬虫-豆瓣电影-" + type + ".txt";
            with open("./bug_file/" + file_name, "a+", encoding="utf-8") as wf:
                for item in json:

                    print(item.get("title")+"爬取成功...");
                    wf.write(str(item) + "\n");

            print(file_name, "爬取完毕!!");
            print(f"本次爬取电影{len(json)}部");
        else:
            print("爬取失败!!");

    else:
          print("爬取结束，已无可爬取数据");


    return len(json);

if __name__ == '__main__':

    if(not os.path.exists("bug_file")):
        os.mkdir("bug_file");

    print("01=>纪录\t 02=>传记\t 03=>犯罪\t 04=>历史\t 05=>动作\t 06=>情色\t 07=>歌舞\t 08=>儿童\r");
    print("10=>悬疑\t 11=>剧情\t 12=>灾难\t 13=>爱情\t 14=>音乐\t 15=>冒险\t 16=>奇幻\t 17=>科幻\r");
    print("18=>运动\t 19=>惊悚\t 20=>恐怖\t 22=>战争\t 23=>短片\t 24=>喜剧\t 25=>动画\t 26=>同性\r");
    print("27=>西部\t 28=>家庭\t 29=>武侠\t 30=>古装\t 31=>黑色电影\r");

    type = input("请选择您想要爬取的影片类型:\n");

    url = "https://movie.douban.com/j/chart/top_list";

    print("================================ 数据爬取中 ================================ \r");

    for index in range(0,1000):
        params = {
            "type": int(type),
            "interval_id": "100:90",  # 好评比率,好于100%-90%的喜剧片
            "action": "",
            "start": index * 20,
            "limit": 20
        };

        num = get_list(url,params);

        if num > 0:
            continue;
        else:
            break;




